package com.hunt.controller;

import com.alibaba.fastjson.JSONArray;
import com.hunt.model.entity.CustomStock;
import com.hunt.model.entity.StockBase;
import com.hunt.model.entity.UserInfo;
import com.hunt.service.CustomStockListService;
import com.hunt.service.UserInfoService;
import com.hunt.util.ResponseCode;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @Author: sjs
 * @Date : 2020/10/19
 */

@Api(value = "手机app端,自选股同步")
@Controller
@RequestMapping(path = "/app/stock")
public class OptionalStockController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(OptionalStockController.class);


    @Autowired
    private CustomStockListService customStockListService;

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "更新自选股", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "updateOptionalStock", method = RequestMethod.POST)
    public Result updateOptionalStock(@RequestParam(value = "userId") String userId,
                                      @RequestParam(value = "stockList") String stockList) {

        CustomStock customStock = customStockListService.findCustomStockListByUserId(userId);
        if (customStock == null) {
            UserInfo userInfo = userInfoService.findUserInfoByUserId(userId);
            if (userInfo == null) {
                return Result.error(ResponseCode.unknown_account.getMsg());
            } else {
                customStockListService.insertCustomStockListByUserId(userId, stockList);
                return Result.success();
            }
        } else {
            customStockListService.updateCustomStockList(userId, stockList);
            return Result.success();
        }
    }

    @ApiOperation(value = "查询自选股", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findOptionalStock", method = RequestMethod.POST)
    public Result findOptionalStock(@RequestParam(value = "userId") String userId) {

        CustomStock customStock = customStockListService.findCustomStockListByUserId(userId);

        if (customStock == null) {
            return Result.success();
        } else {
            //将字符串stockList转成stockInfoList
            if(customStock.getStockList().isEmpty()) {
                return Result.success();
            } else {
                List<StockBase> infoList = JSONArray.parseArray(customStock.getStockList(), StockBase.class);
                return Result.success(infoList);
            }
        }
    }

}
